﻿using System;
using System.Collections.Generic;
using System.Linq;

//客户类
class Customer
{
    public string Name { get; set; }

    public override string ToString() => $"Customer: {Name}";
}

//货物类
class Goods
{
    public string Name { get; set; }
    public double Price { get; set; }

    public override string ToString() => $"Goods: {Name}, Price: {Price}";
}

//订单明细类
class OrderDetails
{
    public Goods Goods { get; set; }
    public int Quantity { get; set; }

    public double TotalPrice => Goods.Price * Quantity;

    public override string ToString() => $"Goods: {Goods.Name}, Quantity: {Quantity}, Total Price: {TotalPrice}";

    public override bool Equals(object obj)
    {
        return obj is OrderDetails details &&
               EqualityComparer<Goods>.Default.Equals(Goods, details.Goods);
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(Goods);
    }
}

//订单类
class Order
{
    public int OrderId { get; set; }
    public Customer Customer { get; set; }
    public List<OrderDetails> Details { get; set; } = new List<OrderDetails>();

    public double TotalAmount => Details.Sum(d => d.TotalPrice);

    public override string ToString()
    {
        return $"Order ID: {OrderId}, Customer: {Customer.Name}, Total Amount: {TotalAmount}\n" +
               $"Details:\n{string.Join("\n", Details)}\n";
    }

    public override bool Equals(object obj)
    {
        return obj is Order order &&
               OrderId == order.OrderId;
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(OrderId);
    }
}

//订单服务类
class OrderService
{
    private List<Order> orders = new List<Order>();

    //增
    public void AddOrder(Order order)
    {
        if (orders.Contains(order))
        {
            throw new InvalidOperationException($"Order with ID {order.OrderId} already exists.");
        }
        orders.Add(order);
    }

    //删
    public void DeleteOrder(int orderId)
    {
        var order = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order == null)
        {
            throw new KeyNotFoundException($"Order with ID {orderId} not found.");
        }
        orders.Remove(order);
    }

    //改
    public void UpdateOrder(Order newOrder)
    {
        var existingOrder = orders.FirstOrDefault(o => o.OrderId == newOrder.OrderId);
        if (existingOrder == null)
        {
            throw new KeyNotFoundException($"Order with ID {newOrder.OrderId} not found.");
        }
        orders.Remove(existingOrder);
        orders.Add(newOrder);
    }

    //查
    public List<Order> QueryOrders(Func<Order, bool> predicate)
    {
        //获取一个根据total排序的订单消息
        var result = orders.Where(predicate).OrderByDescending(o => o.TotalAmount).ToList();
        return result;
    }

    //排序
    public void SortOrders(Func<Order, object> keySelector = null)
    {
        if (keySelector == null)
        {
            orders = orders.OrderBy(o => o.OrderId).ToList();
        }
        else
        {
            orders = orders.OrderBy(keySelector).ToList();
        }
    }

    //获取所有订单
    public List<Order> GetAllOrders() => orders;
}

//test
class Program
{
    static void Main(string[] args)
    {
        OrderService service = new OrderService();

        
        Customer customer1 = new Customer { Name = "A" };
        Customer customer2 = new Customer { Name = "B" };

        Goods goods1 = new Goods { Name = "aaa", Price = 1000 };
        Goods goods2 = new Goods { Name = "bbb", Price = 5000 };
        Goods goods3 = new Goods { Name = "ccc", Price = 3000};

        OrderDetails detail1 = new OrderDetails { Goods = goods1, Quantity = 1 };
        OrderDetails detail2 = new OrderDetails { Goods = goods2, Quantity = 2 };
        OrderDetails detail3 = new OrderDetails { Goods = goods3, Quantity = 3 };

        Order order1 = new Order { OrderId = 1, Customer = customer1 };
        order1.Details.Add(detail1);
        order1.Details.Add(detail2);

        Order order2 = new Order { OrderId = 2, Customer = customer2 };
        order2.Details.Add(detail2);

        Order order3 = new Order { OrderId = 3, Customer = customer1 };
        order3.Details.Add(detail3);

        
        service.AddOrder(order1);
        service.AddOrder(order2);
        service.AddOrder(order3);

        
        Console.WriteLine("所有订单:");
        service.GetAllOrders().ForEach(Console.WriteLine);

        Console.WriteLine("查询A的订单：");
        service.QueryOrders(o => o.Customer.Name == "A").ForEach(Console.WriteLine);

        Console.WriteLine("查询toatl_amount的订单");
        service.QueryOrders(o => o.TotalAmount > 500).ForEach(Console.WriteLine);

        
        Console.WriteLine("删除ID为2的订单");
        try
        {
            service.DeleteOrder(2);
            service.GetAllOrders().ForEach(Console.WriteLine);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        
        Console.WriteLine("更新ID为1的订单：");
        try
        {
            Order updatedOrder = new Order { OrderId = 1, Customer = customer1 };
            updatedOrder.Details.Add(new OrderDetails { Goods = goods3, Quantity = 5 });
            service.UpdateOrder(updatedOrder);
            service.GetAllOrders().ForEach(Console.WriteLine);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

       
        Console.WriteLine("根据total_amount的自定义排序：");
        service.SortOrders(o => o.TotalAmount);
        service.GetAllOrders().ForEach(Console.WriteLine);
    }
}
